﻿Imports System.Collections.Generic
Imports System.Data
Imports System.Data.SqlClient


''' <summary>
''' Clase de la capa de acceso a datos DAL que implementa el patrón Table Data Gateway para el manejo de la entidad Solicitud
''' </summary>
''' <remarks></remarks>
Public Class TDGSolicitud

    Private Shared m_Instance As TDGSolicitud = Nothing


    Public Function Insert(conn As SqlConnection, _
                           Soli_Fecha As DateTime, _
                           TDoc_Codigo As String, _
                           Postu_NroDoc As String, _
                           Busq_Numero As Integer, _
                           Soli_Observaciones As String, _
                           MedB_Codigo As Integer) As Int16
        Dim command As New SqlCommand
        Dim iRowsAffected As Int16

        Dim outParam_Soli_Numero As SqlParameter


        command.Connection = conn
        command.CommandType = CommandType.StoredProcedure

        command.CommandText = "dbo.addSolicitud"
        command.Parameters.AddWithValue("@Soli_Fecha", Soli_Fecha)
        command.Parameters.AddWithValue("@TDoc_Codigo", TDoc_Codigo)
        command.Parameters.AddWithValue("@Postu_NroDoc", Postu_NroDoc)
        command.Parameters.AddWithValue("@Busq_Numero", Busq_Numero)
        command.Parameters.AddWithValue("@Soli_Observaciones", Soli_Observaciones)
        command.Parameters.AddWithValue("@MedB_Codigo", MedB_Codigo)

        outParam_Soli_Numero = command.Parameters.Add("@Soli_Numero", SqlDbType.SmallInt)
        outParam_Soli_Numero.Direction = ParameterDirection.InputOutput


        iRowsAffected = command.ExecuteNonQuery()


        ' Retorno valores
        'MedB_Codigo = outParam_Soli_Numero.Value

        Return iRowsAffected

    End Function

    Public Function Update(conn As SqlConnection, _
                           Soli_Numero As Int32, _
                           Soli_Fecha As DateTime,
                           TDoc_Codigo As String, _
                           Postu_NroDoc As String, _
                           Soli_Observaciones As String, _
                           MedB_Codigo As Integer) As Int16
        Dim command As New SqlCommand
        Dim iRowsAffected As Int16


        command.Connection = conn
        command.CommandType = CommandType.StoredProcedure

        command.CommandText = "dbo.updSolicitud"
        command.Parameters.AddWithValue("@Soli_Numero", Soli_Numero)
        command.Parameters.AddWithValue("@Soli_Fecha", Soli_Fecha)
        command.Parameters.AddWithValue("@TDoc_Codigo", TDoc_Codigo)
        command.Parameters.AddWithValue("@Postu_NroDoc", Postu_NroDoc)
        command.Parameters.AddWithValue("@Soli_Observaciones", Soli_Observaciones)
        command.Parameters.AddWithValue("@MedB_Codigo", MedB_Codigo)


        iRowsAffected = command.ExecuteNonQuery()

        Return iRowsAffected

    End Function

    Public Function Delete(conn As SqlConnection, Soli_Numero As Int32) As Int16
        Dim command As New SqlCommand
        Dim iRowsAffected As Int16

        command.Connection = conn
        command.CommandType = CommandType.StoredProcedure
        command.CommandText = "dbo.delSolicitud"
        command.Parameters.AddWithValue("@Soli_Numero", Soli_Numero)

        iRowsAffected = command.ExecuteNonQuery()

        Return iRowsAffected

    End Function

    Public Function Find(conn As SqlConnection, Soli_Numero As Int32) As SqlDataReader
        Dim command As New SqlCommand
        Dim dr As SqlDataReader


        command.Connection = conn
        command.CommandType = CommandType.StoredProcedure

        command.CommandText = "dbo.getSolicitud"
        command.Parameters.AddWithValue("@Soli_Numero", Soli_Numero)


        dr = command.ExecuteReader()

        Return dr

    End Function

    Public Function FindAllPorBusqueda(conn As SqlConnection, Busq_Numero As Int16) As SqlDataReader
        Dim command As New SqlCommand
        Dim dr As SqlDataReader


        command.Connection = conn
        command.CommandType = CommandType.StoredProcedure

        command.CommandText = "dbo.getSolicitud_PorBusqueda"

        command.Parameters.AddWithValue("@Busq_Numero", Busq_Numero)

        dr = command.ExecuteReader()

        Return dr

    End Function

    Public Shared ReadOnly Property Instance()
        Get
            If (m_Instance Is Nothing) Then
                m_Instance = New TDGSolicitud()
            End If

            Return m_Instance
        End Get
    End Property

End Class
